package com.itheima.entry;

import com.itheima.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class SystemEntry {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();
        //死循环
        loop:
        while (true) {

            //键盘录入
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            //选择
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("新增");
                    addStudent(list);
                    break;
                case "2":
                    //System.out.println("删除");
                    deleteStudent(list);
                    break;
                case "3":
                    //System.out.println("修改");
                    updateStudent(list);
                    break;
                case "4":
                    // System.out.println("查看");
                    findAllStudents(list);
                    break;
                case "5":
                    System.out.println("再见");
                    //break loop;
                    return;
                default:
                    System.out.println("输入有误");
                    break;
            }
        }
    }

    //新增
    public static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学号");
        String sid = null;
        while (true) {
            //判断学号是否存在
            sid = sc.next();
            int index = getIndex(sid, list);
            if (index < 0) {
                //可以录
                break;
            } else {
                System.out.println("已经存在请重新录入");
            }
        }
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入生日");
        String birthday = sc.next();
        Student student = new Student(sid, name, age, birthday);
        list.add(student);
        System.out.println("添加成功");

    }

    //删除
    public static void deleteStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学号");
        String sid = null;
        while (true) {
            //判断学号是否存在
            sid = sc.next();
            int index = getIndex(sid, list);
            if (index >= 0) {
                //可以删
                list.remove(index);
                System.out.println("删除成功");
                break;
            } else {
                System.out.println("不存在请重新录入");
            }
        }

    }

    //修改
    public static void updateStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学号");
        String sid = null;
        while (true) {
            //判断学号是否存在
            sid = sc.next();
            int index = getIndex(sid, list);
            if (index >= 0) {
                //可以改
                System.out.println("请输入姓名");
                String name = sc.next();
                System.out.println("请输入年龄");
                int age = sc.nextInt();
                System.out.println("请输入生日");
                String birthday = sc.next();
                Student student = new Student(sid, name, age, birthday);
                list.set(index, student);
                System.out.println("修改成功");
                break;
            } else {
                System.out.println("不存在请重新录入");
            }
        }
    }

    //查看
    public static void findAllStudents(ArrayList<Student> list) {
        if (list == null || list.size() == 0) {
            System.out.println("没有数据请先添加");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            System.out.println(student.getSid() + "\t" + student.getName() + "\t" + student.getAge() + "岁\t" + student.getBirthday());

        }
    }

    //定义方法 判断学号是否存在
    public static int getIndex(String sid, ArrayList<Student> list) {

        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if (student.getSid().equals(sid)) {
                return i;
            }
        }
        return -1;


    }


}
